/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webidewar.jsf;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.WebIDE.ejb.entities.Users;
import org.WebIDE.ejb.sessions.*;

/**
 *
 * @author huy
 */
public class UsersConverter implements Converter {
    public Object getAsObject(FacesContext facesContext, UIComponent component, String string) {
        if (string == null || string.length() == 0) {
            return null;
        }
        String id = string;
        UsersFacadeRemote controller = (UsersFacadeRemote) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "usersJpa");
        //UsersFacadeRemote controller = con.getFacade();
        return controller.findUsers(id);
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Users) {
            Users o = (Users) object;
            return o.getUserName() == null ? "" : o.getUserName().toString();
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: org.WebIDE.ejb.entities.Users");
        }
    }

}
